<?php

namespace app\models;

use Yii;
use yii\base\Exception;
use yii\base\Model;

/**
 * 卡分配表单
 * Class CardDispatchForm
 * @package app\models
 */
class OrderForm extends Model
{
    /**
     * @var string 卡来源 iccid range search
     */
    public $source;
    /**
     * @var string 多条ICCID
     */
    public $iccid_list;
    /**
     * @var string ICCID开始号码
     */
    public $iccid_start;
    /**
     * @var string ICCID结束号码
     */
    public $iccid_end;
    /**
     * @var integer 搜索接口编号
     */
    public $search_api_id;
    /**
     * @var integer 搜索运营商编号
     */
    public $search_isp_id;
    /**
     * @var string 搜索批次号
     */
    public $search_batch_no;
    /**
     * @var string 用户类型 agent user
     */
    public $user_type;
    /**
     * @var string 用户名
     */
    public $username;
    /**
     * @var Agent $agent 操作代理商
     */
    public $agent;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['source', 'default', 'value' => 'iccid'],
            [['iccid_list', 'iccid_start', 'iccid_end'], 'safe'],
            [['search_api_id', 'search_isp_id'], 'integer'],
            [['source', 'user_type', 'username'], 'required'],
            ['user_type', 'default', 'value' => 'user'],
            ['search_batch_no', 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'source' => '来源',
            'iccid_list' => 'ICCID列表',
            'iccid_start' => 'ICCID开始号码',
            'iccid_end' => 'ICCID结束号码',
            'search_api_id' => '搜索接口',
            'search_isp_id' => '搜索运营商',
            'search_batch_no' => '搜索批次号',
            'user_type' => '用户类型',
            'username' => '用户名',
        ];
    }
    /**
     * 根据id 及类型查 名字
     * @return boolean
     */
    public function nameget($type,$id){
        //echo "type:".$type;echo "<br>";
        //echo "id:".$id;echo "<br>";
        if($type=='user'){
            $user = User::findOne(['id' => $id, 'status' => User::STATUS_OK]);
        }else{
            $user = Agent::findOne(['id' => $id, 'status' => Agent::STATUS_OK]);
        }
        //pr($user);
        return $user->username;
    }
    /**
     * 根据名字及类型查id
     * @return boolean
     */
    public function useridget($type,$name){
        if($type=='user'){
            $user = User::findOne(['username' => $name, 'status' => User::STATUS_OK]);
        }else{
            $user = Agent::findOne(['username' => $name, 'status' => Agent::STATUS_OK]);
        }
        return $user->id;
    }
    /**
     * 分配
     * @return boolean
     */
    public function dispatch()
    {
        if (!$this->validate()) {
            return false;
        }
        if (!empty($this->agent)) {
            // 代理商分配卡，需要判断分配用户具有从属关系
            if ($this->user_type == 'user') {
                $user = User::findOne(['username' => $this->username, 'status' => User::STATUS_OK]);
                if ($user->agent_id != $this->agent->id) {
                    $this->addError('username', '没有找到用户。');
                }
            }
            if ($this->user_type == 'agent') {
                $agent = Agent::findOne(['username' => $this->username, 'status' => Agent::STATUS_OK]);
                if (!Agent::checkRelation($this->agent->id, $agent->id)) {
                    $this->addError('username', '没有找到代理商。');
                }
            }
            if ($this->hasErrors()) {
                return false;
            }
        }

        return false;
    }
}
